package net.huashitong.supervision.gongjian.service.schedule;

import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.service.RedisService;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.gongjian.entity.ProjectTaskEntity;
import net.huashitong.supervision.gongjian.entity.project.*;
import net.huashitong.supervision.gongjian.filter.*;
import net.huashitong.supervision.gongjian.service.GjDeptProjectService;
import net.huashitong.supervision.gongjian.service.ProjectTaskService;
import net.huashitong.supervision.gongjian.service.project.*;
import net.huashitong.supervision.task.service.SMSUtilService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class GongjianTaskScheduleService {
    
    @Autowired
    private ProjectTaskService projectTaskService;
    @Autowired
    private ZhengQianService zhengqianservice;
    @Autowired
    private KaiGongService kaiGongService;
    @Autowired
    private ZaijianTisuService zaijianTisuService;
    @Autowired
    private JungongTouchanService jungongService;
    @Autowired
    private ZhaoShangProjectService zhaoshangService;
    @Autowired
    private SMSUtilService  smsUtilService;
    @Autowired
    private GjDeptProjectService deptProjectService;
    @Autowired
    private RedisService redisService;


    /**
     * 每个月 1号生成新任务 ，旧任务时效  15号提醒逾期   16号生成新任务旧任务时效。 最后一天提醒逾期；
     * 每隔15天产生一天 攻坚任务 并且通知主要领导
     */

    public  void  saveGongjianTask(){

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        if(day==1||day==16){

                /*
                所有任务旧任务时效
             */
            ProjectTaskFilter zhengqianFilter = new ProjectTaskFilter();
            zhengqianFilter.setValid(1);
            List<ProjectTaskEntity> list = projectTaskService.getListNoPage(zhengqianFilter);
            if(ObjectUtils.isNotBlank(list))
            {
                list.forEach(o->o.setValid(0));
                projectTaskService.batchUpdate(list);

            }

        }



        /*
         * 征迁项目
         */
        List<ZhengQianEntity> zhengQianEntityList = zhengqianservice.getListNoPage(new ZhengQianFilter());
        HashMap<String, Object> projectDeptMap=new HashMap<>();
        for (ZhengQianEntity item : zhengQianEntityList) {

            /*
                生成新任务
             */
            ProjectTaskEntity taskEntity = projectTaskService.saveTask(item.getSysId(), 1);
            projectDeptMap = deptProjectService.getProjectDept(projectDeptMap,item.getSysId(), 2, 1);

        }




        List<KaiGongEntity> kaigongList = kaiGongService.getListNoPage(new KaiGongFilter());
        for (KaiGongEntity item : kaigongList) {



            ProjectTaskEntity taskEntity = projectTaskService.saveTask(item.getSysId(), 2);
            projectDeptMap = deptProjectService.getProjectDept(projectDeptMap,item.getSysId(), 2, 2);

        }

        /*
         * 在建项目
         */
        List<ZaijianTisuEntity> zaijianList = zaijianTisuService.getListNoPage(new ZaijianTisuFilter());

        for (ZaijianTisuEntity item : zaijianList) {



            ProjectTaskEntity taskEntity = projectTaskService.saveTask(item.getSysId(), 3);
            projectDeptMap = deptProjectService.getProjectDept(projectDeptMap,item.getSysId(), 2, 3);

        }
        /*
         * 竣工项目
         */

        List<JungongTouchanEntity> jungongList = jungongService.getListNoPage(new JungongTouchanFilter());

        for (JungongTouchanEntity item : jungongList) {



            ProjectTaskEntity taskEntity = projectTaskService.saveTask(item.getSysId(), 4);
            projectDeptMap = deptProjectService.getProjectDept(projectDeptMap,item.getSysId(), 2, 4);

        }

        /*
        招商
         */
        List<ZhaoShangProjectEntity> zhaoshangList = zhaoshangService.getListNoPage(new ZhaoShangProjectFilter());
        for (ZhaoShangProjectEntity item : zhaoshangList) {
            ProjectTaskEntity taskEntity = projectTaskService.saveTask(item.getSysId(), 5);
            projectDeptMap = deptProjectService.getProjectDept(projectDeptMap,item.getSysId(), 2, 5);

        }


       
        /*
            获取短信联系人
         */
        List<SysUser> listUser= new ArrayList<>();
        for (String key:projectDeptMap.keySet()){
            listUser = deptProjectService.getDeptUser(listUser,key, "1");

        }
        /*
        发送短信
         */
        for (SysUser user : listUser) {
            sendMessage(user.getMobile(),user.getName());
        }



    }

    public void  sendMessage(String tel,String userName){
        String msg="督查管理系统】您好，您的攻坚120天专项行动相关任务已经生成并且派发至贵单位账号，请及时登陆网页版督查系统进行办理。";

//        类型 1立项 2 批转  3 催办  4逾期  5逾期前提醒 6 攻坚120天任务生成提醒 7 攻坚120任务逾期提醒
        smsUtilService.taskRemindMessage(tel,userName,msg,"6");
    }
    

}
